package com.kobe.game_40;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * The fraction ^( 49 ) /_( 98 ) is a curious fraction, as an inexperienced
 * mathematician in attempting to simplify it may incorrectly believe that ^( 49 )
 * /_( 98 ) = ^( 4 ) /_( 8 ) , which is correct, is obtained by cancelling the
 * 9s.
 * 
 * We shall consider fractions like, ^( 30 ) /_( 50 ) = ^( 3 ) /_( 5 ) , to be
 * trivial examples.
 * 
 * There are exactly four non-trivial examples of this type of fraction, less
 * than one in value, and containing two digits in the numerator and
 * denominator.
 * 
 * If the product of these four fractions is given in its lowest common terms,
 * find the value of the denominator.
 * 
 */
public class Game33 {
    public static void main(String[] args) {
        int[] returnValue = new int[2];
        for (int a = 11; a <= 98; a++) {
            for (int b = a + 1; b <= 98; b++) {
                if (hasOneDigitalConflictAndDeleteed(a, b, returnValue)) {
                    if (returnValue[1] != 0 && a % 10 != 0) {
                        double x = (double) a / (double) b;
                        double y = (double) returnValue[0]
                                / (double) returnValue[1];
                        if (x == y) {
                            System.out.println(returnValue[0] + " "
                                    + returnValue[1]);
                        }
                    }
                }
            }
        }
    }

    public static boolean hasOneDigitalConflictAndDeleteed(int a, int b,
            int[] returnValue) {
        List<Character> aList = new ArrayList<Character>();
        List<Character> bList = new ArrayList<Character>();
        List<Character> conflicts = new ArrayList<Character>();
        boolean hasConflict = false;
        for (char tempA : String.valueOf(a).toCharArray()) {
            aList.add(tempA);
        }
        for (char tempB : String.valueOf(b).toCharArray()) {
            bList.add(tempB);
        }
        for (char tempA : aList) {
            for (char tempB : bList) {
                if (tempA == tempB) {
                    conflicts.add(tempA);
                    hasConflict = true;
                }
            }
        }
        for (Character temp : conflicts) {
            aList.remove(temp);
            bList.remove(temp);
        }
        if (hasConflict && aList.size() != 0 & bList.size() != 0) {
            returnValue[0] = Character.getNumericValue(aList.get(0));
            returnValue[1] = Character.getNumericValue(bList.get(0));
        } else {
            return false;
        }
        return hasConflict;
    }
}
